package com.service.pay.diamond.service;

import com.service.pay.diamond.config.RechargeConfig;
import com.service.pay.diamond.dao.OrderDao;
import com.service.pay.diamond.dao.RechargeSettingDao;
import com.service.pay.diamond.dto.recharge.RechargeDto;
import com.service.pay.diamond.dto.webpay.WebPayNotifyDto;
import com.service.pay.diamond.dto.webpay.WxUnifiedOrderDto;
import com.service.pay.diamond.dto.webpay.WxUnifiedResultDto;
import com.service.pay.diamond.event.RechargeOrderEvent;
import com.service.pay.diamond.model.OrderModel;
import com.service.pay.diamond.model.RechargeSettingModel;
import com.tool.base.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class RechargeService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private WebPayService webPayService;
    @Autowired
    private RechargeSettingDao settingDao;
    @Autowired
    private ApplicationContext context;
    @Autowired
    private RechargeConfig rechargeConfig;

    /*生成充值订单*/
    public Object unifiedOrder(RechargeDto rechargeDto) {
        // 读取支付价格
        RechargeSettingModel settingModel = settingDao.getById(rechargeDto.getId());
        if (null == settingModel) {
            throw new ApiException("充值价格不正确", 400);
        }
        if ("weixin".equals(rechargeDto.getPayment())) {
            return wxUnifiedOrder(settingModel);
        }
        return null;
    }

    /*微信充值*/
    protected WxUnifiedResultDto wxUnifiedOrder(RechargeSettingModel settingModel) {
        WxUnifiedOrderDto wxUnifiedOrderDto = new WxUnifiedOrderDto();
        wxUnifiedOrderDto.setAppid("app_1");
        wxUnifiedOrderDto.setAttach("充值钻石 充值金额 " + settingModel.getPrice());
        wxUnifiedOrderDto.setBody("充值钻石 充值金额 " + settingModel.getPrice());
        wxUnifiedOrderDto.setFee(settingModel.getCount() * 100);
        wxUnifiedOrderDto.setIp("127.0.0.1");
        wxUnifiedOrderDto.setTimeStart(new Date());
        wxUnifiedOrderDto.setTimeEnd(new Date());
        wxUnifiedOrderDto.setSource("钻石充值");
        wxUnifiedOrderDto.setPayment("weixin");
        wxUnifiedOrderDto.setNotifyUrl(rechargeConfig.getNotifyUrl());
        wxUnifiedOrderDto.setOrderNo(UUID.randomUUID().toString().substring(0, 32));
        WxUnifiedResultDto resultDto = webPayService.unifiedOrder(wxUnifiedOrderDto);
        log.warn("resultDto {}", resultDto);
        return resultDto;
    }

    /*处理充值订单*/
    public void rechargeOrder(OrderModel orderModel) {
        orderModel.setStatus(1);
        orderModel.setPayDateTime(new Date());
        orderDao.save(orderModel);
        context.publishEvent(new RechargeOrderEvent(this, orderModel));
    }

    /*处理充值回调*/
    public void handleNotify(WebPayNotifyDto notifyDto) {
        log.warn("接收充值回调 {}", notifyDto);
        OrderModel orderModel = orderDao.getByOrder(notifyDto.getOrderNo());
        if (!"recharge".equals(orderModel.getType())) {
            throw new ApiException("订单类型不正确", 500);
        }

        if (1 == orderModel.getStatus()) {
            throw new ApiException("订单已支付", 500);
        }
        if (1 == notifyDto.getStatus()) {
            rechargeOrder(orderModel);
        }
    }
}
